package com.fenyin.samples.study.mongo.example1;  

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;

/**
 * <p>类:Mongo</p>
 * <p>类:DB</p>
 * <p>类:DBCollection</p>
 * <p>类:BasicDBObject</p>
 *@Title:  mongodb例子
 *@Description:  简单客户端操作
 *@Author:fengzhichao  
 *@Since:2012-9-25  
 *@Version:1.1.0
 */
public class SimpleClient {
	
	private static final Log log = LogFactory.getLog(SimpleClient.class);
	
	private Mongo mongo;
	
	private DB db;
	
	private DBCollection collection;
	
	
	public SimpleClient() throws UnknownHostException, MongoException{
//		mongo = new Mongo("127.0.0.1",29010);
		mongo = new Mongo("192.168.119.190",10011);
		//设置安全写模式
		mongo.setWriteConcern(WriteConcern.SAFE);
		//获取指定的数据库
//		db = mongo.getDB("km_mongodb");
		//获取指定的集合
//		collection = db.getCollection("user");
	}
	
	public void print(){
		//获取所有的数据库名称
		List<String> dbNames = mongo.getDatabaseNames();
		for(String dbName : dbNames){
			log.info("dbName:"+dbName);
		}
		
		//获取所有的集合名称
		Set<String> collectionNames = db.getCollectionNames();
		for(String collectionName : collectionNames){
			log.info("dbName:km_mongodb:collectionName:"+collectionName);
		}
	}
	
	public int add(){
		
		DBObject  document = new BasicDBObject();
		document.put("_id", "1");
		document.put("name", "tomcat");
		document.put("age", 30);
		
		DBObject  location = new BasicDBObject();
		location.put("x", 101);
		location.put("y", 202);
		
		document.put("location", location);
		return collection.insert(document).getN();
	}
	
	public int addList(){
		List<DBObject> documentList = new ArrayList<DBObject>();
		for(int i=0;i<5;i++){
			DBObject  document = new BasicDBObject();
			document.put("name", "tomcat"+i);
			document.put("age", 30+i);
			documentList.add(document);
		}
		return collection.insert(documentList).getN();
	}
	
	public int delete(){
		return collection.remove(queryOne()).getN();
	}
	
	public int deleteByCondition(){
		DBObject condition = new BasicDBObject("age",new BasicDBObject("$gte",33));
		return collection.remove(condition).getN();
	}
	
	public int update(){
		DBObject data = queryOne();
		data.put("name", "tomcat");
		data.put("age", 1000);
		return collection.update((DBObject)data.get("_id"), data).getN();
	}
	
	public List<DBObject> queryAll(){
		List<DBObject> documentList = new ArrayList<DBObject>();
		DBCursor cursor = collection.find();
		while(cursor.hasNext()){
			documentList.add(cursor.next());
		}
		return documentList;
	}
	
	public DBObject queryOne(){
		DBCursor cursor = collection.find();
		while(cursor.hasNext()){
			return cursor.next();
		}
		return null;
	}
	
	public void addCappedCollection(){
		DBObject  options = new BasicDBObject();
		options.put("capped", true);
		options.put("size", 1000);
		options.put("max", 1000);
		collection = db.createCollection("infoAnalyzeLog_TEMP", options);
		
	}
	
	public static void main(String[] args){
//		simpleClient.print();
		try{
			SimpleClient simpleClient = new SimpleClient();
//			log.info(simpleClient.add());
			simpleClient.addCappedCollection();
		}catch(MongoException.DuplicateKey ex){
			log.info(ex.getCode());
			log.info(ex);
		}catch(Throwable th){
			log.info(th);
		}
//		log.info(simpleClient.addList());
//		log.info(simpleClient.delete());
//		log.info(simpleClient.queryAll());
	}
}
